package com.arcaneshift.manager.action.game;

import java.util.ArrayList;
import java.util.List;

import com.arcaneshift.dao.action.game.CreatureCardDao;
import com.arcaneshift.dao.interfaces.ICreatureCardDao;
import com.arcaneshift.manager.interfaces.ICreatureCardManager;
import com.arcaneshift.model.action.game.CreatureCard;
import com.arcaneshift.security.exceptions.CardException;
import com.common.dao.interfaces.IDao;
import com.common.dao.util.Criteria;
import com.common.dao.util.QueryOperation;
import com.common.manager.abstracts.AbstractManager;
import com.common.security.exceptions.DatabaseException;
import com.common.security.exceptions.ManagerException;

public class CreatureCardManager extends AbstractManager<CreatureCard> implements ICreatureCardManager {

	private ICreatureCardDao creatureDao;
	
	public CreatureCardManager() {
		creatureDao = new CreatureCardDao();
	}
	
	@Override
	protected IDao<CreatureCard> getDao() {
		return creatureDao;
	}

	/**
	 * Check if a Creature Card with the provided name exists in the database
	 * 
	 * @param name
	 * @return true/false
	 * @throws ManagerException.DATABASE_EXCEPTION
	 */
	public boolean exist(String name) throws ManagerException {
		try {
			List<Criteria> criterias = new ArrayList<Criteria>();
			criterias.add(new Criteria(CreatureCard.NAME, name, QueryOperation.EQUALS, true));
			return creatureDao.getByCriteria(criterias).size() > 0;
		} catch (DatabaseException e) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	/**
	 * 
	 * @param name
	 * @return
	 * @throws ManagerException.DATABASE_EXCEPTION
	 * @throws CardException.CARD_NOT_FOUND
	 * @throws CardException.MULTIPLE_CARDS_FOUND
	 */
	public CreatureCard getCreatureCard(String name) throws ManagerException {
		try {
			List<Criteria> criterias = new ArrayList<Criteria>();
			criterias.add(new Criteria(CreatureCard.NAME, name, QueryOperation.EQUALS, true));
			List<CreatureCard> creatures = creatureDao.getByCriteria(criterias);
			
			if (creatures.size() == 0) {
				throw new CardException(CardException.CARD_NOT_FOUND);
			} else if (creatures.size() == 1) {
				return creatures.get(0);
			} else {
				throw new CardException(CardException.MULTIPLE_CARDS_FOUND);
			}
		} catch (DatabaseException databaseException) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
}
